﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ManagedWinapi;

namespace wutils
{
    public class Configuration
    {
        private static List<Configuration> configurations = new List<Configuration>();
        private List<Hotkey> hotkeys = new List<Hotkey>();

        #region Hotkeys

        public Hotkey ToTray { get; set; }
        public Hotkey OnTop { get; set; }
        public Hotkey TaskMan { get; set; }
        public Hotkey RestoreAll { get; set; }
        public Hotkey ToggleTaskStrip { get; set; }
        public Hotkey Exit { get; set; }

        #endregion

        #region Keys

        public Keys ToTrayKey
        {
            get { return ToTray.KeyCode; }
            set { ToTray.KeyCode = value; }
        }
        public Keys OnTopKey
        {
            get { return OnTop.KeyCode; }
            set { OnTop.KeyCode = value; }
        }
        public Keys TaskManKey
        {
            get { return TaskMan.KeyCode; }
            set { TaskMan.KeyCode = value; }
        }
        public Keys RestoreAllKey
        {
            get { return RestoreAll.KeyCode; }
            set { RestoreAll.KeyCode = value; }
        }
        public Keys TaskStripKey
        {
            get { return ToggleTaskStrip.KeyCode; }
            set { ToggleTaskStrip.KeyCode = value; }
        }
        public Keys ExitKey
        {
            get { return Exit.KeyCode; }
            set { Exit.KeyCode = value; }
        }

        #endregion

        #region Events

        //public EventHandler ToTrayPressed
        //{
        //    get { return ToTray.HotkeyPressed; }
        //    set { ToTray.KeyCode = value; }
        //}
        //public EventHandler OnTopPressed
        //{
        //    get { return OnTop.KeyCode; }
        //    set { OnTop.KeyCode = value; }
        //}
        //public EventHandler TaskManKey
        //{
        //    get { return TaskMan.KeyCode; }
        //    set { TaskMan.KeyCode = value; }
        //}
        //public EventHandler RestoreAllPressed
        //{
        //    get { return RestoreAll.KeyCode; }
        //    set { RestoreAll.KeyCode = value; }
        //}
        //public EventHandler ToggleTaskStripPressed
        //{
        //    get { return ToggleTaskStrip.KeyCode; }
        //    set { ToggleTaskStrip.KeyCode = value; }
        //}
        //public EventHandler ExitPressed
        //{
        //    get { return Exit.KeyCode; }
        //    set { Exit.KeyCode = value; }
        //}

        #endregion

        public bool ShowTaskStrip { get; set; }
        public Size StripSize { get; set; }
        public Point StripLocation { get; set; }

        public Configuration()
        {
            ShowTaskStrip = false;

            StripLocation=new Point(100,100);
            StripSize = new Size(749, 39);

            ToTray = new Hotkey();
            OnTop = new Hotkey();
            TaskMan = new Hotkey();
            RestoreAll = new Hotkey();
            ToggleTaskStrip = new Hotkey();
            Exit = new Hotkey();

            hotkeys.Add(ToTray);
            hotkeys.Add(OnTop);
            hotkeys.Add(TaskMan);
            hotkeys.Add(RestoreAll);
            hotkeys.Add(ToggleTaskStrip);
            hotkeys.Add(Exit);

            configurations.Add(this);

            InitializeHotkeys();
        }

        private void InitializeHotkeys()
        {
            foreach (Hotkey hotkey in hotkeys)
            {
                hotkey.WindowsKey = true;
                hotkey.Enabled = false;
            }

            ToTray.KeyCode = Keys.V;
            OnTop.KeyCode = Keys.C;
            TaskMan.KeyCode = Keys.Q;
            RestoreAll.KeyCode = Keys.X;
            ToggleTaskStrip.KeyCode = Keys.S;
            Exit.KeyCode = Keys.Z;
        }

        private void EnableHotkeys()
        {
            DisableHotkeys();
            foreach (Hotkey hotkey in hotkeys)
            {
                try
                {
                    hotkey.Enabled = true;
                }
                catch (HotkeyAlreadyInUseException ex)
                {
                    ex.Data.Add("message", "Hotkey Windows+" + hotkey.KeyCode + " is in use!");
                    ex.Data.Add("keyCode", hotkey.KeyCode);
                    throw;
                }
            }
        }
        private void DisableHotkeys()
        {
            foreach (Hotkey hotkey in hotkeys)
            {
                hotkey.Enabled = false;
            }
        }

        public void EnableConfiguration()
        {
            foreach (Configuration configuration in configurations)
            {
                configuration.DisableConfiguration();
            }

            EnableHotkeys();
        }
        public void DisableConfiguration()
        {
            DisableHotkeys();
        }

        public static Configuration LoadConfiguration()
        {
            //Mock
            return LoadDefaultConfiguration();
        }
        public static Configuration LoadDefaultConfiguration()
        {
            Configuration config = new Configuration { };
            return config;
        }
        public void SaveConfiguration()
        {
            //Mock
        }

        ~Configuration()
        {
            DisableHotkeys();
            configurations.Remove(this);
        }
    }
}
